<?php


namespace app\cms\home\api\saas;


use app\cms\authCallBack\saas;
use app\cms\community\lib\saas\initializationSaas;
use app\cms\dto\saas\saasClientInfo;
use app\cms\model\saas\saasClientModel;
use app\cms\model\saas\saasClientServerDbModel;
use app\cms\model\saas\saasClientServerModel;
use iflow\auth\lib\authAnnotation;
use iflow\Response;
use iflow\Router\Controller;
use iflow\Router\implement\Request\{ GetMapping, PostMapping };

#[Controller('%cmsHome%/saas')]
class saasAccount
{
    /**
     * 创建Saas服务
     * @param saasClientInfo $saasClientInfo
     * @return Response
     */
    #[PostMapping]
    #[authAnnotation('saasUser', '*', saas::class . '@validIsSaasAccountValidServer')]
    public function createSaasAccount(saasClientInfo $saasClientInfo): Response {
        return (new initializationSaas(
            $saasClientInfo -> server_db_id, $saasClientInfo -> server_id,
        )) -> initialization($saasClientInfo);
    }

    #[PostMapping]
    #[authAnnotation('saasUser', '*', saas::class . '@validIsSaasAccountValidServer')]
    public function leaseRenewalSaasAccount() {
        // TODO: 续费Saas服务
    }

    /**
     * 获取Saas 服务账户列表
     * @return Response
     * @throws \Exception
     */
    #[GetMapping]
    #[authAnnotation('saasUser', '*', saas::class . '@validIsSaasAccountValidServer')]
    public function getSaasAccount(): Response
    {
        $uid = session('userInfo')['uid'];
        return (new saasClientModel()) -> getSaasClientList(uid: $uid);
    }

    /**
     * 更新Saas服务
     * @param saasClientInfo $saasClientInfo
     * @return Response
     */
    #[PostMapping]
    #[authAnnotation('saasUser', '*', saas::class . '@validIsSaasAccountValidServer')]
    public function updateSaas(saasClientInfo $saasClientInfo): Response
    {
        if ($saasClientInfo -> saas_id === 0)
            return message() -> parameter_error('请选择需要更新的服务');

        $saas = (new saasClientModel()) -> getSaasInfo(
            uid: $saasClientInfo -> getUid(), saas_id: $saasClientInfo -> saas_id
        );

        if (count($saas) === 0) return message() -> parameter_error('当前所选服务不存在');

        return (new initializationSaas(
            $saasClientInfo -> server_db_id, $saasClientInfo -> server_id, true
        )) -> initialization($saasClientInfo);
    }

    /**
     * 获取指定saas数据
     * @param int $saas_id
     * @return Response
     */
    #[GetMapping]
    #[authAnnotation('saasUser', '*', saas::class . '@validIsSaasAccountValidServer')]
    public function getSaasServiceInfo(int $saas_id): Response
    {
        $uid = session('userInfo')['uid'];
        $saas = (new saasClientModel()) -> getSaasInfo(uid: $uid, saas_id: $saas_id);
        return $saas ? message() -> success('查询成功', $saas) : message() -> nodata('暂无数据');
    }

    /**
     * 获取 服务器列表数据
     * @param string $server_name
     * @return Response
     */
    #[GetMapping]
    #[authAnnotation('saasUser', '*', saas::class . '@validIsSaasAccountValidServer')]
    public function getSaasServer(string $server_name): Response
    {
        return (new saasClientServerModel()) -> getSaasClientServerList($server_name, status: 1);
    }

    /**
     * 获取数据库列表
     * @param string $server_db_name
     * @return Response
     */
    #[GetMapping]
    #[authAnnotation('saasUser', '*', saas::class . '@validIsSaasAccountValidServer')]
    public function getSaasDbServer(string $server_db_name): Response
    {
        return (new saasClientServerDbModel()) -> getSaasClientDbServer($server_db_name, status: 1);
    }
}